import { connect } from 'umi';
import { Spin } from 'antd';
import { useEffect, useState } from 'react';
import '../../common/style.less';
import { getClarifyServices, syncPDFtoOAClarifyServices } from '../../services/UpdateService';
import { IClarificationControllerContext } from '../../common/useOAContext';
import { DetailInfoTopViewOA } from './DetailInfoTopViewOA';
import { DetailInfoCenterViewOA } from './DetailInfoCenterViewOA';
import { DetailInfoBottomViewOA } from './DetailInfoBottomViewOA';
import { getPosIDs, getTenantID, getUserID, renderWaterMarkDom } from '@/utils';
import { RevokeModal } from '@/pages/PurchaseManage/Acomponents/RevokeModal';
import { ILoadMapTypeAll } from '@/common';
import { setOAMessageSev, SHTG_CHECKSTATE, SHZ_CHECKSTATE } from '@/pages/PurchaseManage/Acommon';

const DetailInfoViewOA: React.FC<Record<string, any>> = ({ locationProps, curRes }) => {
  /** 样式根名称 */
  const prefix = 'purchase-clarification-update-';

  const [infoObj, setInfoObj] = useState<Record<string, any>>({}); /** 保存详情 */
  const [invalidState, setInvalidState] = useState<Record<string, any>>({}); /**撤销弹窗*/
  const [isSHTGFlag, setIsSHTGFlag] = useState(false); /** 审核中，审核通过开关 */

  /** 消息已读已办接口 */
  const { setOAMessageRun, setOAMessageLoaidng } = setOAMessageSev(() => {});

  /**获取详情*/
  const { getClarifyRun, getClarifyLoading, reloadDetailInfo } = getClarifyServices((res) => {
    if (res?.msgFlag) {
      setInfoObj(res?.obj);
      // 做已读办理的接口处理
      let infoObject = res?.obj;
      if (infoObject && infoObject?.paramObject?.[`${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1009`]) {
        let workFlowNode = infoObject?.paramObject?.[`${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1009`];
        if (
          workFlowNode &&
          workFlowNode?.mactivitiTaskList?.length > 0 &&
          workFlowNode?.mactivitiTaskList?.[0]?.assigneePositionID == getPosIDs() &&
          workFlowNode?.mactivitiTaskList?.[0]?.assigneeID == getUserID()
        ) {
          setOAMessageRun({
            // 已读未办
            id: locationProps?.state?.taskId,
            isRemark: 0,
            viewType: 1,
          });
        }
      }
    }
  });

  /** 初始化 */
  useEffect(() => {
    if (locationProps?.state?.id) {
      getClarifyRun({ id: locationProps?.state?.id });
    }
  }, [locationProps?.state?.id]);

  // 初始化水印
  // useEffect(() => {
  //   renderWaterMarkDom('contract_oa_page', true);
  //   return () => {
  //     renderWaterMarkDom('contract_oa_page', false);
  //   };
  // }, []);

  /** 同步审核记录给档案 */
  const { syncPDFtoOARun, syncPDFtoOALoading } = syncPDFtoOAClarifyServices((res) => {});
  const syncPDFtoOARunfun = (infoObj) => {
    let mactivitiHisTaskInstList =
      infoObj?.paramObject?.[`${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1009`]?.mactivitiHisProcinstList?.[0]?.mactivitiHisTaskInstList;
    let resObj = { ...infoObj, paramObject: { mactivitiHisTaskInstList } };
    syncPDFtoOARun(resObj);
  };

  /** 监听数据状态变化 */
  useEffect(() => {
    if (infoObj?.state && infoObj?.checkState == SHZ_CHECKSTATE()) {
      setIsSHTGFlag(true);
    }
    if (infoObj?.state && infoObj?.checkState == SHTG_CHECKSTATE()) {
      if (isSHTGFlag) {
        syncPDFtoOARunfun(infoObj);
      }
    }
  }, [infoObj?.state, infoObj?.checkState]);

  return (
    <IClarificationControllerContext.Provider
      value={{
        styleFixCls: prefix,
        infoObj,
        reloadDetail: reloadDetailInfo,
        curRes,
        invalidState,
        setInvalidState,
        setOAMessageRun,
        taskId: locationProps?.state?.taskId,
      }}
    >
      <Spin spinning={getClarifyLoading || setOAMessageLoaidng || syncPDFtoOALoading}>
        <div className={`${prefix}infoViewOA`}>
          <DetailInfoTopViewOA />
          <DetailInfoCenterViewOA />
          <DetailInfoBottomViewOA />
        </div>
      </Spin>

      {invalidState?.isShow && (
        <RevokeModal
          resObj={invalidState?.resObj}
          revokeRequestUrl={'/purchaseSourceClarifyController/invalid'}
          onClose={() => setInvalidState({})}
          reqBody={{ state: `${getTenantID()}${ILoadMapTypeAll.PROCURECLARFY_STATE}1000` }}
          modalTip={
            <div style={{ color: '#000' }}>
              确认是否撤销【<span style={{ color: 'red' }}>{invalidState?.resObj?.purchProjectName}</span>】的审核, 撤销后可重新提交！
            </div>
          }
          reloadRequest={reloadDetailInfo}
        />
      )}
    </IClarificationControllerContext.Provider>
  );
};

export default connect(({ baseLogin }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(DetailInfoViewOA);
